//
//  BBBattoryProgressView.m
//  BabyThermometer
//
//  Created by CaiLei on 10/3/14.
//  Copyright (c) 2014 baohu. All rights reserved.
//

#import "BBBattoryProgressView.h"
@interface BBBattoryProgressView ()
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIImageView *progressImageView;
@end

@implementation BBBattoryProgressView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        UIImage *bgImage = [[UIImage imageNamed:@"side_battery_progress_bg"] stretchableImageWithLeftCapWidth:30 topCapHeight:0];
        UIImage *progressImage = [[UIImage imageNamed:@"side_battery_progress"] stretchableImageWithLeftCapWidth:30 topCapHeight:0];
        self.bgImageView = [[UIImageView alloc] initWithImage:bgImage];
        self.progressImageView = [[UIImageView alloc] initWithImage:progressImage];

        [self addSubview:self.bgImageView];
        [self addSubview:self.progressImageView];
        _progress = 0.f;
    }
    return self;
}

- (void)setProgress:(CGFloat)progress {
    _progress = MIN(1, MAX(0, progress));
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bgImageView.frame = self.bounds;
    CGFloat w = self.bounds.size.width;
    w *= self.progress;
    self.progressImageView.frame = CGRectMake(0, 0, w, self.bounds.size.height);
}

@end
